package net.cityside.summer.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

@Controller
@RequestMapping("locale")
public class ChangeLocaleController {

	@RequestMapping("change/{newLocale}")
	public String change(@PathVariable String newLocale,
			HttpServletRequest request, HttpServletResponse response) {
		if (newLocale != null) {
			LocaleResolver localeResolver = RequestContextUtils
					.getLocaleResolver(request);
			if (localeResolver == null) {
				throw new IllegalStateException(
						"No LocaleResolver found: not in a DispatcherServlet request?");
			}
			localeResolver.setLocale(request, response,
					StringUtils.parseLocaleString(newLocale));
		}
		return "redirect:/index.html"; // 如果不加/，相对于但前路径，即到/local/change/index.html
	}

}
